<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-flash.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
    
  
  <link href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.2.5/jquery.fancybox.min.css" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  
    
      
    

    
  

  

  

  
    
      
    

    
  

  
    
    
    <link href="https://fonts.cat.net/css?family=Lato:300,300italic,400,400italic,700,700italic|Roboto Slab:300,300italic,400,400italic,700,700italic|Roboto Mono:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="cordova,vue," />










<meta name="description" content="开始前, 先吐槽一波, 这段话后面的”,”后面的空格看见了么, 这是ES6的语法, 折磨我半天.开始前,先吐槽一波,这段话后面的”,”后面的空格看见了么,这是ES6的语法,折磨我半天.没有对比就没有伤害, 心在滴血😂N 年不用的js,差一点 就 LFAT, 我的元旦三天假…😂">
<meta name="keywords" content="cordova,vue">
<meta property="og:type" content="article">
<meta property="og:title" content="ios集成cordova自定义插件以及在vue中的使用.">
<meta property="og:url" content="http:&#x2F;&#x2F;urm9ril.top&#x2F;2018&#x2F;01&#x2F;04&#x2F;how-to-use-cordova-custom-plugin-in-vue&#x2F;index.html">
<meta property="og:site_name" content="ooops 的小站">
<meta property="og:description" content="开始前, 先吐槽一波, 这段话后面的”,”后面的空格看见了么, 这是ES6的语法, 折磨我半天.开始前,先吐槽一波,这段话后面的”,”后面的空格看见了么,这是ES6的语法,折磨我半天.没有对比就没有伤害, 心在滴血😂N 年不用的js,差一点 就 LFAT, 我的元旦三天假…😂">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-06-28T06:22:26.192Z">
<meta name="twitter:card" content="summary">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"hide","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":true,"transition":{"post_block":"perspectiveLeftIn","post_header":"fadeIn","post_body":"fadeIn","coll_header":"perspectiveLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://urm9ril.top/2018/01/04/how-to-use-cordova-custom-plugin-in-vue/"/>





  <title>ios集成cordova自定义插件以及在vue中的使用. | ooops 的小站</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">ooops 的小站</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"><_`_></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" target="_blank" rel="noopener" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://urm9ril.top/2018/01/04/how-to-use-cordova-custom-plugin-in-vue/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="张强">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="ooops 的小站">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">ios集成cordova自定义插件以及在vue中的使用.</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-01-04T21:15:20+08:00">
                2018-01-04
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/cordova/" itemprop="url" rel="index">
                    <span itemprop="name">cordova</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2018/01/04/how-to-use-cordova-custom-plugin-in-vue/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2018/01/04/how-to-use-cordova-custom-plugin-in-vue/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          
             <span id="/2018/01/04/how-to-use-cordova-custom-plugin-in-vue/" class="leancloud_visitors" data-flag-title="ios集成cordova自定义插件以及在vue中的使用.">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">热度&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
                 <span>℃</span>
             </span>
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1,821
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  8
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h5 id="开始前-先吐槽一波-这段话后面的”-”后面的空格看见了么-这是ES6的语法-折磨我半天"><a href="#开始前-先吐槽一波-这段话后面的”-”后面的空格看见了么-这是ES6的语法-折磨我半天" class="headerlink" title="开始前, 先吐槽一波, 这段话后面的”,”后面的空格看见了么, 这是ES6的语法, 折磨我半天."></a>开始前, 先吐槽一波, 这段话后面的”,”后面的空格看见了么, 这是ES6的语法, 折磨我半天.</h5><h5 id="开始前-先吐槽一波-这段话后面的”-”后面的空格看见了么-这是ES6的语法-折磨我半天-1"><a href="#开始前-先吐槽一波-这段话后面的”-”后面的空格看见了么-这是ES6的语法-折磨我半天-1" class="headerlink" title="开始前,先吐槽一波,这段话后面的”,”后面的空格看见了么,这是ES6的语法,折磨我半天."></a>开始前,先吐槽一波,这段话后面的”,”后面的空格看见了么,这是ES6的语法,折磨我半天.</h5><h2 id="没有对比就没有伤害-心在滴血😂"><a href="#没有对比就没有伤害-心在滴血😂" class="headerlink" title="没有对比就没有伤害, 心在滴血😂"></a>没有对比就没有伤害, 心在滴血😂</h2><h2 id="N-年不用的js-差一点-就-LFAT-我的元旦三天假…😂"><a href="#N-年不用的js-差一点-就-LFAT-我的元旦三天假…😂" class="headerlink" title="N 年不用的js,差一点 就 LFAT, 我的元旦三天假…😂"></a>N 年不用的js,差一点 就 LFAT, 我的元旦三天假…😂</h2><a id="more"></a>
<h6 id="———————————-华丽丽的开始了-准备好你们的道具-————————————"><a href="#———————————-华丽丽的开始了-准备好你们的道具-————————————" class="headerlink" title="——————————— 华丽丽的开始了,准备好你们的道具 ————————————"></a>——————————— 华丽丽的开始了,准备好你们的道具 ————————————</h6><p>##1.项目创建<br>基于 <code>webpack</code> 模板创建一个 <code>vue</code> 项目</p>
<p><code>cordova : cordova create hello com.example.hello HelloWord</code></p>
<ul>
<li>第一个参数 <code>hello</code> 表示在工程目录中创建一个 <code>hello</code> 的文件夹</li>
<li>第二个参数 <code>com.example.hello</code> 表示包名（反向域名），用于标志不同的 <code>app</code></li>
<li>第三个参数 <code>HelloWord</code>表示项目的名称，可以在 <code>config.xml</code> 文件中修改</li>
</ul>
<p>创建基于 <code>webpack</code> 模板的  <code>vue</code>  项目</p>
<p><code>vue : vue init webpack my-project</code></p>
<p>##2.添加cordova项目的平台支持</p>
<p>2.1 进入创建的项目目录<br><code>cd hello</code></p>
<p>2.2 查看已有的平台<br><code>cordova platforms list</code></p>
<p>2.3添加所需要的平台<br><code>cordova platform add android</code><br><code>cordova platform add ios</code></p>
<p>2.4如果想移除已经添加的平台的话<br><code>cordova platform remove android</code> or <code>cordova platform rm android</code></p>
<p>##3.pluman 的安装</p>
<p>3.1安装 <code>plugman</code><br><code>npm install -g plugman</code></p>
<p>3.2创建 <code>plugman</code> 插件 语法如下:<br><code>plugman create --name &lt;pluginName&gt; --plugin_id &lt;pluginID&gt; --plugin_version &lt;version&gt; [--path &lt;directory&gt;] [--variableNAME=VALUE]</code></p>
<p><code>egg : plugman create --name hello --plugin_id firstPlugin --plugin_version 0.0.1</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">参数:</span><br><span class="line">pluginName: 插件名字</span><br><span class="line">pluginID: 插件id, egg : coolPlugin </span><br><span class="line">oversion: 版本, egg : 0.0.1</span><br><span class="line">directory:一个绝对或相对路径的目录，该目录将创建插件项目</span><br><span class="line">variable NAME=VALUE: 额外的描述，如作者信息和相关描述</span><br></pre></td></tr></table></figure>
<p>回到 <code>cordova</code> 创建项目目录下 找到 <code>platforms</code> 下 的 <code>ios / android</code> </p>
<p>项目在 <code>plugin</code>  目录下 编写需要的 <code>native</code>文件 (<code>native</code> 文件也可以后面写) </p>
<p>将编写好的 <code>native</code> 插件 放入 <code>cordova</code> 插件下的 <code>src</code> 目录 </p>
<p>规范化的处理是 建立平台文件夹 放入 对应的平台文件夹中 这里 以 <code>iOS</code> 为例</p>
<p>检查 <code>plugin.xml</code> 配置文件相关节点的配置<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version='1.0' encoding='utf-8'?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">plugin</span> <span class="attr">id</span>=<span class="string">"firstPlugin"</span> <span class="attr">version</span>=<span class="string">"0.0.1"</span> <span class="attr">xmlns</span>=<span class="string">"http://apache.org/cordova/ns/plugins/1.0"</span> <span class="attr">xmlns:android</span>=<span class="string">"http://schemas.android.com/apk/res/android"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">name</span>&gt;</span>hello<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">js-module</span> <span class="attr">name</span>=<span class="string">"hello"</span> <span class="attr">src</span>=<span class="string">"www/hello.js"</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- &lt;clobbers target="cordova.plugins.hello" /&gt; --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">clobbers</span> <span class="attr">target</span>=<span class="string">"navigator.hello"</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">js-module</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">platform</span> <span class="attr">name</span>=<span class="string">"ios"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">config-file</span> <span class="attr">target</span>=<span class="string">"config.xml"</span> <span class="attr">parent</span>=<span class="string">"/*"</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">feature</span> <span class="attr">name</span>=<span class="string">"HelloWord"</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">param</span> <span class="attr">name</span>=<span class="string">"ios-package"</span> <span class="attr">value</span>=<span class="string">"CDVHelloWord"</span>/&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">param</span> <span class="attr">name</span>=<span class="string">"onload"</span> <span class="attr">value</span>=<span class="string">"true"</span> /&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">feature</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">config-file</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">header-file</span> <span class="attr">src</span>=<span class="string">"src/ios/CDVHelloWord.h"</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">source-file</span> <span class="attr">src</span>=<span class="string">"src/ios/CDVHelloWord.m"</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">platform</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>这样创建的插件 是不能安装的 缺少 <code>package.json</code> 文件</p>
<p>执行 <code>plugman createpackagejson 插件目录</code> </p>
<p>生成 <code>package.json</code> 插件信息文件(terminal会让你输入一些东西) </p>
<p>如下: 前两行是创建插件的时候 自带的信息 没有发布到 <code>git</code> 所以可以没填</p>
<pre><code>name: (firstPlugin) 
version: (0.0.1) 

description: 测试插件
git repository: 
author: 348043793@qq.com
license: (ISC) MIT

插件添加正常 提示如下 :
name: (coolPlugin) 
version: (0.0.1) 
description: 测试插件
git repository: 
author: 348043793@qq.com
license: (ISC) MIT


About to write to /Users/ooops/Desktop/oc_vue/package.json:

{
  &quot;name&quot;: &quot;coolPlugin&quot;,
  &quot;version&quot;: &quot;0.0.1&quot;,
  &quot;description&quot;: &quot;测试插件&quot;,
  &quot;cordova&quot;: {
    &quot;id&quot;: &quot;coolPlugin&quot;,
    &quot;platforms&quot;: [
      &quot;ios&quot;
    ]
  },
  &quot;keywords&quot;: [
    &quot;ecosystem:cordova&quot;,
    &quot;cordova-ios&quot;
  ],
  &quot;author&quot;: &quot;348043793@qq.com&quot;,
  &quot;license&quot;: &quot;MIT&quot;
}


Is this ok? (yes)
</code></pre><h2 id="编写插件代码"><a href="#编写插件代码" class="headerlink" title="编写插件代码"></a>编写插件代码</h2><p>这个根据业务 / 逻辑 需要自行编写 (请自行使用 <code>百度</code> <code>google</code> 一下), 放出一个简单的示例<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> exec = <span class="built_in">require</span>(<span class="string">'cordova/exec'</span>); <span class="comment">// 固定格式</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> options = &#123;<span class="attr">quality</span>: <span class="string">'200'</span>&#125;;     <span class="comment">// 没什么卵用的参数</span></span><br><span class="line"><span class="comment">// exports.sayHi 扩展一个插件的方法</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*   &lt;feature name="HelloWord"&gt;</span></span><br><span class="line"><span class="comment">        &lt;param name="ios-package" value="CDVHelloWord"/&gt;</span></span><br><span class="line"><span class="comment">        &lt;param name="onload" value="true" /&gt;</span></span><br><span class="line"><span class="comment">    &lt;/feature&gt;</span></span><br><span class="line"><span class="comment">* </span></span><br><span class="line"><span class="comment">* exec(success, error, 'HelloWord', 'sayHello', [options]);</span></span><br><span class="line"><span class="comment">* success       成功回调,</span></span><br><span class="line"><span class="comment">* error         失败回调,</span></span><br><span class="line"><span class="comment">* HelloWord     上面 feature 中的 name,</span></span><br><span class="line"><span class="comment">* sayHello      CDVHelloWord(原生文件)中的函数名 / 方法名, 需要传递给原生的参数(字典 / 字典数组)  </span></span><br><span class="line"><span class="comment">* exec(success, error, 'HelloWord', 'sayHello', [options]);</span></span><br><span class="line"><span class="comment">*/</span> </span><br><span class="line">exports.sayHi = <span class="function"><span class="keyword">function</span> (<span class="params">options, success, error</span>) </span>&#123;</span><br><span class="line">    exec(success, error, <span class="string">'HelloWord'</span>, <span class="string">'sayHello'</span>, [options]);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h2 id="安装编写的插件"><a href="#安装编写的插件" class="headerlink" title="安装编写的插件"></a>安装编写的插件</h2><p><code>cd</code> 插件目录   </p>
<p>执行 <code>pwd</code> 得到当前插件目录   <code>/Users/ooops/Desktop/plugins/hello</code></p>
<p><code>cd cordova</code> 项目目录</p>
<p>执行 <code>cordova plugin add 插件目录</code></p>
<pre><code>输出信息 如下 表示插件安装成功 :

Installing &quot;SimpleMath&quot; for android
Android Studio project detected
Installing &quot;SimpleMath&quot; for ios
Adding SimpleMath to package.json
Saved plugin info for &quot;SimpleMath&quot; to config.xml
</code></pre><h2 id="vue-cordova-插件的使用"><a href="#vue-cordova-插件的使用" class="headerlink" title="vue-cordova 插件的使用"></a>vue-cordova 插件的使用</h2><h3 id="在线安装-这个不做介绍了-坑了我两天"><a href="#在线安装-这个不做介绍了-坑了我两天" class="headerlink" title="在线安装 - 这个不做介绍了 坑了我两天"></a>在线安装 - 这个不做介绍了 坑了我两天</h3><h3 id="本地安装"><a href="#本地安装" class="headerlink" title="本地安装"></a>本地安装</h3><ul>
<li>为什么要本地安装？</li>
<li>现有的插件不够，如果要调用自己开发的插件，在线的不能用，只能将项目放在Vue项目中根据实际情况自 己添加插件</li>
</ul>
<p>如果已安装了 <code>vue-cordova</code> ,控制台执行 <code>npm uninstall vue-cordova</code> 删除掉</p>
<p>在 <code>github</code> 上 <code>clone</code> 下 <code>vue-cordova</code> 项目</p>
<pre><code>git clone https://github.com/kartsims/vue-cordova.git
</code></pre><p>在 <code>Vue</code> 项目的 <code>src</code> 目录下新建一个 <code>cordova</code> 目录</p>
<p>将下载的 <code>vue-cordova src</code>目录下的 <code>index.js</code> copy 到 <code>Vue</code> 项目新建的 <code>cordova</code> </p>
<p>目录下面，命名为 <code>cordova-plugin-index.js</code> ,不要使用 <code>index.js</code> ，否则会出现命名冲突</p>
<p><code>main.js</code> 的导包改成下面的样子：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> VueCordova <span class="keyword">from</span> <span class="string">'./cordova/cordova-plugin-index.js'</span></span><br><span class="line">Vue.use(VueCordova)</span><br></pre></td></tr></table></figure>
<p><code>App.vue</code> 中 需要<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br></pre></td></tr></table></figure></p>
<p>到这里 环境基本搞好了 </p>
<h2 id="如何安装本地插件"><a href="#如何安装本地插件" class="headerlink" title="如何安装本地插件"></a>如何安装本地插件</h2><p>在 <code>Vue</code> 项目文件的 <code>cordova</code> 目录下新建一个 <code>plugins</code> 目录</p>
<ul>
<li>在 <code>src/cordova/plugins/</code> 目录下新建 <code>cordova-plugin-sayhi.js</code> ,<br>按照 <code>camera</code> 插件仿造的一个 <code>js</code> 文件</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">exports.install = <span class="function"><span class="keyword">function</span> (<span class="params">Vue, options, cb</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">document</span>.addEventListener(<span class="string">'deviceready'</span>, () =&gt; &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> navigator.hello === <span class="string">'undefined'</span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> cb(<span class="literal">false</span>)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 关键点</span></span><br><span class="line">    Vue.cordova.hello = navigator.hello</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> cb(<span class="literal">true</span>)</span><br><span class="line">  &#125;, <span class="literal">false</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li><p>其中 <code>navigator.hello</code> 为 插件的 <code>config.xml</code> 中的 <code>&lt;clobbers target=&quot;navigator.hello&quot; /&gt;</code> 节点中的内容</p>
</li>
<li><p>将自定义的插件 添加到 <code>cordova-plugin-index.js</code> 中 如下位置</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> pluginsList = [</span><br><span class="line">  <span class="string">'cordova-plugin-sayhi'</span></span><br><span class="line">]</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>Vue</code> 中调用插件</p>
</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">v-on:click</span>=<span class="string">"click"</span>&gt;</span>Add 1<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span> <span class="comment">// 这个很重要, 否则会报 Vue is undefined</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  name: <span class="string">'app'</span>,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    click: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="built_in">window</span>.alert(Vue);</span><br><span class="line">      Vue.cordova.hello.saiHi(&#123;<span class="attr">quality</span>: <span class="string">'200'</span>&#125;, <span class="function"><span class="keyword">function</span> (<span class="params">success</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">window</span>.alert(success);</span><br><span class="line">      &#125;, <span class="function"><span class="keyword">function</span> (<span class="params">error</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">window</span>.alert(error)</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>存在的问题</p>
<ul>
<li>上述解决方案 适用于 <code>vue</code> 整个项目打包 扔进 <code>app</code> 使用 <code>webpack</code> 打包 <code>app</code> </li>
<li><p>如果 <code>H5</code> 集成的过多 <code>app</code> 安装包势必会越来越大…….</p>
</li>
<li><p>请看下面的描述</p>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">猜想</span><br><span class="line"></span><br><span class="line">找不到插件, 插件是存在的,只是他找不到,</span><br><span class="line">调用插件是需要依赖库的,em 貌似很有道理</span><br><span class="line">插件需要依赖的文件,先导入看看.</span><br><span class="line"></span><br><span class="line"># 验证猜想 </span><br><span class="line"> </span><br><span class="line">尝试1 </span><br><span class="line">window.alert(Vue.cordova.plugins);</span><br><span class="line">在 index.html文件中手动加入 cordova.js</span><br><span class="line">打印 插件数组 ...............ok - ☺</span><br><span class="line">如何让他 找到这个插件....继续摸索</span><br><span class="line">糊里糊涂的  又引用了 exec.js 文件</span><br><span class="line"></span><br><span class="line">解决: </span><br><span class="line">打包整个项目放到服务器找不到插件 在index.html中 添加如下两个文件的引用</span><br><span class="line">&lt;script type=text/javascript src=cordova.js&gt;&lt;/script&gt;</span><br><span class="line">&lt;script type=text/javascript src=cordova-js-src/exec.js&gt;&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">// 这两句没什么卵用 - 有没有一样?</span><br><span class="line">&lt;script type=text/javascript src=cordova-js-src/platform.js&gt;&lt;/script&gt;</span><br><span class="line">&lt;script type=text/javascript src=cordova_plugins.js&gt;&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<h2 id="至此-Vue-和-native-可以交互-至于姿势-自由选择-可以解锁"><a href="#至此-Vue-和-native-可以交互-至于姿势-自由选择-可以解锁" class="headerlink" title="至此 Vue 和 native 可以交互 - 至于姿势 自由选择 可以解锁"></a>至此 Vue 和 native 可以交互 - 至于姿势 自由选择 可以解锁</h2><h6 id="—————————————-就这么华丽丽的结束了-—————————————"><a href="#—————————————-就这么华丽丽的结束了-—————————————" class="headerlink" title="—————————————- 就这么华丽丽的结束了 —————————————-"></a>—————————————- 就这么华丽丽的结束了 —————————————-</h6><p>参考链接</p>
<p><a href="https://cn.vuejs.org/v2/guide/events.html" target="_blank" rel="noopener" title="Vue Document">Vue Document</a>    </p>
<p><a href="http://kartsims.github.io/vue-cordova/" target="_blank" rel="noopener" title="vue-cordova Document">vue-cordova Document</a>     </p>

      
    </div>
    
    
    

    

    <div>
      
        
      
    </div>

    

    

    

    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束  <i class="fa-window-minimize"></i>  ß感谢您的阅读-------------</div>
    
</div>
      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/cordova/" <i class="fa fa-tags"></i>cordova</a>
          
            <a href="/tags/vue/" <i class="fa fa-tags"></i>vue</a>
          
        </div>
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/12/18/attributestring/" rel="next" title="iOS AttributeString">
                <i class="fa fa-chevron-left"></i> iOS AttributeString
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/01/27/custom-uicontrol-uilabel/" rel="prev" title="iOS 自定义控件-UILabel">
                iOS 自定义控件-UILabel <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">张强</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/%20%7C%7C%20archive">
              
                  <span class="site-state-item-count">13</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">16</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

            <div id="days"></div>
    </script>
    <script language="javascript">
    function show_date_time(){
        window.setTimeout("show_date_time()", 1000);
        BirthDay = new Date("06/10/2014 15:00:00");
        today = new Date();
        timeold = (today.getTime() - BirthDay.getTime());
        sectimeold = timeold / 1000
        secondsold = Math.floor(sectimeold);
        msPerDay = 24 * 60 * 60 * 1000
        e_daysold = timeold / msPerDay
        daysold = Math.floor(e_daysold);
        e_hrsold = (e_daysold - daysold) * 24;
        hrsold = setzero(Math.floor(e_hrsold));
        e_minsold = (e_hrsold - hrsold) * 60;
        minsold = setzero(Math.floor((e_hrsold - hrsold) * 60));
        seconds = setzero(Math.floor((e_minsold - minsold) * 60));
        document.getElementById('days').innerHTML = "已运行" + daysold + "天" + hrsold + "小时" + minsold + "分" + seconds + "秒";
    }
    function setzero(i) {
        if (i<10)
        {i="0" + i};
        return i;
    }
    show_date_time();
</script>

          </nav>

          

          

          
          

          
          
        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-5"><a class="nav-link" href="#开始前-先吐槽一波-这段话后面的”-”后面的空格看见了么-这是ES6的语法-折磨我半天"><span class="nav-number">1.</span> <span class="nav-text">开始前, 先吐槽一波, 这段话后面的”,”后面的空格看见了么, 这是ES6的语法, 折磨我半天.</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#开始前-先吐槽一波-这段话后面的”-”后面的空格看见了么-这是ES6的语法-折磨我半天-1"><span class="nav-number">2.</span> <span class="nav-text">开始前,先吐槽一波,这段话后面的”,”后面的空格看见了么,这是ES6的语法,折磨我半天.</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#没有对比就没有伤害-心在滴血😂"><span class="nav-number"></span> <span class="nav-text">没有对比就没有伤害, 心在滴血😂</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#N-年不用的js-差一点-就-LFAT-我的元旦三天假…😂"><span class="nav-number"></span> <span class="nav-text">N 年不用的js,差一点 就 LFAT, 我的元旦三天假…😂</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#———————————-华丽丽的开始了-准备好你们的道具-————————————"><span class="nav-number">0.1.</span> <span class="nav-text">——————————— 华丽丽的开始了,准备好你们的道具 ————————————</span></a></li></ol></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#编写插件代码"><span class="nav-number"></span> <span class="nav-text">编写插件代码</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#安装编写的插件"><span class="nav-number"></span> <span class="nav-text">安装编写的插件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#vue-cordova-插件的使用"><span class="nav-number"></span> <span class="nav-text">vue-cordova 插件的使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#在线安装-这个不做介绍了-坑了我两天"><span class="nav-number"></span> <span class="nav-text">在线安装 - 这个不做介绍了 坑了我两天</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#本地安装"><span class="nav-number"></span> <span class="nav-text">本地安装</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#如何安装本地插件"><span class="nav-number"></span> <span class="nav-text">如何安装本地插件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#至此-Vue-和-native-可以交互-至于姿势-自由选择-可以解锁"><span class="nav-number"></span> <span class="nav-text">至此 Vue 和 native 可以交互 - 至于姿势 自由选择 可以解锁</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#—————————————-就这么华丽丽的结束了-—————————————"><span class="nav-number">0.1.</span> <span class="nav-text">—————————————- 就这么华丽丽的结束了 —————————————-</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2015 &mdash; <span itemprop="copyrightYear">2019</span>
  <!--<span class="with-love">
    <i class="fa fa-heart"></i>
  </span>-->
  <span class="with-love">
  (●'◡'●)ﾉ♥
  </span>
  <span class="author" itemprop="copyrightHolder">张强</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">全站总字数&#58;</span>
    
    <span title="全站总字数">16.3k</span>
  
</div>









        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      你是本站第<span class="busuanzi-value" id="busuanzi_value_site_uv"></span>位访客
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      文章共批阅<span class="busuanzi-value" id="busuanzi_value_site_pv"></span>次
      
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.2.5/jquery.fancybox.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = '';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: '7FNrT8jBTCLil5J7vFWmPVe3-gzGzoHsz',
        appKey: 'Evbd5Bv3xxN8M3GybtBOyHXU',
        placeholder: '畅所欲言,请尽可能留下真实的邮箱,以便交流.',
        avatar:'wavatar',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("7FNrT8jBTCLil5J7vFWmPVe3-gzGzoHsz", "Evbd5Bv3xxN8M3GybtBOyHXU");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">

  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "default";
      
          pbOptions.boxForm = "vertical";
      
          pbOptions.position = "topCenter";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
  </script>

  

  

  

  


  <!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-backstretch/2.0.4/jquery.backstretch.min.js"></script>;
  <script>
    $(document).ready(function(){
      $('body').backstretch([
        {url: "/images/background.png", fade: 4000},
        {url: "/images/background01.jpg", fade: 4000},
        {url: "/images/background02.jpg", fade: 4000},
        {url: "/images/background03.jpg", fade: 4000}
      ]);
    });
  </script> -->

</body>
</html>
